import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/data/request/intell_request.dart';
import 'package:bxultimate/domain/common/MapLatLonDto.dart';
import 'package:bxultimate/domain/intell/IntellArea.dart';
import 'package:bxultimate/domain/intell/IntellCategory.dart';
import 'package:bxultimate/domain/intell/IntellPublishDto.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/src/components/cell/td_cell.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'state.dart';

class PublishIntellController extends GetxController {
  final PublishIntellState state = PublishIntellState();

  // 发布的类
  IntellPublishDto publishDto = IntellPublishDto();

  List<IntellArea> intellAreaList = [];

  MapLatLonDto mapLatLonDto = MapLatLonDto();
  TextEditingController typeController = TextEditingController();
  TextEditingController unitController = TextEditingController();
  TextEditingController phoneController = TextEditingController();
  TextEditingController detailController = TextEditingController();
  TextEditingController timeController = TextEditingController();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    IntellCategory arguments = Get.arguments;

    publishDto.qingbaoTypeCode = arguments.id;
    publishDto.qingbaoSourceCode = 1;
    typeController.text = arguments.name!;

    var now = DateTime.now();
    state.upTime = '${now.year.toString().padLeft(4, '0')}-'
        '${now.month.toString().padLeft(2, '0')}-'
        '${now.day.toString().padLeft(2, '0')} '
        '${now.hour.toString().padLeft(2, '0')}:'
        '${now.minute.toString().padLeft(2, '0')}:'
        '${now.second.toString().padLeft(2, '0')}';
  }

  void publish() {
    publishDto.userPhone = phoneController.text;
    publishDto.detail = detailController.text;

    if(state.addressName == ''){
      SmartDialog.showNotify(msg: '请选择地理位置', notifyType: NotifyType.failure);
      return;
    }
    if(state.upUnit==''){
      SmartDialog.showNotify(msg: '请选择上报单位', notifyType: NotifyType.failure);
      return;
    }

    if(phoneController.text.isEmpty){
      SmartDialog.showNotify(msg: '请输入手机号', notifyType: NotifyType.failure);
      return;
    }

    if (state.imgList.length == 0) {
      SmartDialog.showNotify(msg: '请上传图片', notifyType: NotifyType.failure);
      return;
    } else {
      publishDto.img = state.imgList;
    }

    if(state.videoList.length >0){
      publishDto.video = state.videoList[0];
    }

    IntellRequest.publishIntell(publishDto).then((value) {
      SmartDialog.showNotify(msg: '发布成功', notifyType: NotifyType.success);
      Get.back();
    });
  }

  // 地址选择
  Future<void> addressSelect(TDCell cell) async {
    mapLatLonDto = await Get.toNamed(RouterName.commonMapPointSelect);
    state.addressName = mapLatLonDto.address;
    // 获取上报单位

    // 设置经纬度
    publishDto.lat = mapLatLonDto.lat.toString();
    publishDto.lon = mapLatLonDto.lon.toString();
    publishDto.position = mapLatLonDto.address;

    intellAreaList =
        await IntellRequest.getAreaSubByAreaCode(mapLatLonDto.postcode);
  }

  // 上报单位的选择
  void showUpUnitSelect(TDCell cell) {
    List<String> data = intellAreaList.map((e) => e.allPath!).toList();
    TDPicker.showMultiPicker(Get.context, title: '选择上报单位',
        onConfirm: (selected) {
      state.upUnit = intellAreaList[selected[0]].allPath;


      publishDto.unitName = intellAreaList[selected[0]].allPath;
      publishDto.qingbaoBusinessType =
          intellAreaList[selected[0]].qingbaoBusinessType;
      publishDto.areaLsgx = intellAreaList[selected[0]].subCode;
      Get.back();
    }, data: [data]);
  }

  void showUpTimeSelect(TDCell cell) {
    var now = DateTime.now();
    TDPicker.showDatePicker(Get.context, title: '选择时间', onConfirm: (selected) {
      state.upTime = '${selected['year'].toString().padLeft(4, '0')}-'
          '${selected['month'].toString().padLeft(2, '0')}-'
          '${selected['day'].toString().padLeft(2, '0')} '
          '${selected['hour'].toString().padLeft(2, '0')}:'
          '${selected['minute'].toString().padLeft(2, '0')}:'
          '${selected['second'].toString().padLeft(2, '0')}';
      Get.back();
    }, useHour: true, useMinute: true, useSecond: true, initialDate: [
      now.year,
      now.month,
      now.day,
      now.hour,
      now.minute,
      now.second
    ]);
  }
}
